#include "EnterPasswordWindow.h"
#include "main.h"

EnterPasswordWindow::EnterPasswordWindow(QWidget *parent) :
_parent(parent)
{
	setWindowIcon(QIcon("./images/Agora.png"));
	display();
	this->show();
}

void EnterPasswordWindow::keyPressEvent(QKeyEvent *e)
{
	if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
		joinChannel();
	QWidget::keyPressEvent(e);
}


void		EnterPasswordWindow::display()
{
	this->setMinimumSize(320, 115);
	this->setMaximumSize(320, 115);
			//IP GroupBox
	QGroupBox *chanInfoBox = new QGroupBox(this);
	chanInfoBox->setTitle("Please enter the password");
	chanInfoBox->setGeometry(10, 10, 300, 60);
	QVBoxLayout	*chanInfoVBox = new QVBoxLayout();

	QLabel *chanPasswordLabel = new QLabel(tr("Channel Password : "));
	_chanPassword = new QLineEdit();
	_chanPassword->setEchoMode(QLineEdit::Password);
	QHBoxLayout *chanPasswordHBox = new QHBoxLayout();
	chanPasswordHBox->addWidget(chanPasswordLabel);
	chanPasswordHBox->addWidget(_chanPassword);

	chanInfoVBox->addLayout(chanPasswordHBox);
	chanInfoBox->setLayout(chanInfoVBox);

	QPushButton* buttonJoin = new QPushButton("Join this channel", this);
	buttonJoin->setGeometry(10, 75, 300, 30);

	connect(buttonJoin, SIGNAL(clicked()), this, SLOT(joinChannel()));
}

void EnterPasswordWindow::joinChannel()
{
	dynamic_cast<Main*>(this->_parent)->MoveToSelectedChannelWithPass(_channelId, _chanPassword->text().toStdString());
	this->hide();
}

void EnterPasswordWindow::setChannelId(int ChannelId)
{
	_channelId = ChannelId;
}